/**
 * <p>Copyright (c) 2011 深圳市鹏途信息技术有限公司 </p>
 * <p>				   All right reserved. 		     </p>
 * 
 * <p>项目名称 ： 	东莞交通工程质量监督综合业务系统        </p>
 * <p>创建者   :	Administrator 
 * 
 * <p>描   述  :   PilespotDao.java for com.pengtu.dao.detect    </p>
 * 
 * <p>最后修改 : $: 2011-8-17-上午10:57:23 v 1.0.0	 Administrator   $     </p>
 * 
 */

package com.pengtu.dao.detect;

import java.io.Serializable;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import com.pengtu.dao.hibernate.HibernateDao;
import com.pengtu.entity.detect.PileSpot;
import com.pengtu.utils.DateUtils;

/**
 * 
 * PilespotDao
 * 
 * 2011-8-17 上午10:57:23
 * 
 * @version 1.0.0
 * 
 */
@Repository
public class PilespotDao extends HibernateDao<PileSpot, Serializable> {

	public void save(PileSpot entity) {
		if (StringUtils.isEmpty(entity.getId())) {
			entity.setId(null);
			entity.setCreateDate(DateUtils.getCurrentDate());
		}
		super.save(entity);
	}

	public List<PileSpot> select(String batch, String projectId, String startTime, String endTime) {
		String hql = "select new com.pengtu.entity.detect.PileSpot(" +
		"sum(u.count),sum(u.pass1),sum(u.pass2),sum(u.pass3)," +
		"sum(u.pass4),sum(u.pass5),sum(u.pass6)) from PileSpot u where 1 = 1";
		if(StringUtils.isNotEmpty(batch)){
			hql += " and u.spotReport.batch = '" + batch + "'";
		}
		
		if(StringUtils.isNotEmpty(projectId)){
			hql += " and u.projectId = '" + projectId +"'";
		}
		if(StringUtils.isNotEmpty(startTime)){
			hql += " and u.reportdate >= to_date('"+startTime+"','yyyy-MM-dd')";
		}
		if(StringUtils.isNotEmpty(endTime)){
			hql += " and u.reportdate < to_date('"+endTime+"','yyyy-MM-dd')";
		}
		if(StringUtils.isNotEmpty(projectId)){
			hql +=  " group by u.projectId ";
		}
		return this.find(hql);
	}
}
